package org.jeecg.modules.externalEquity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.config.JeecgBaseConfig;
import org.jeecg.modules.externalEquity.entity.TbExternalEquity;
import org.jeecg.modules.externalEquity.service.ITbExternalEquityService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 2、对外股权投资
 * @Author: jeecg-boot
 * @Date: 2025-04-23
 * @Version: V1.0
 */
@Api(tags = "2、对外股权投资")
@RestController
@RequestMapping("/externalEquity/tbExternalEquity")
@Slf4j
public class TbExternalEquityController extends JeecgController<TbExternalEquity, ITbExternalEquityService> {
    @Autowired
    private ITbExternalEquityService tbExternalEquityService;

    /**
     * 分页列表查询
     *
     * @param tbExternalEquity
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "4、对外股权投资-分页列表查询")
    @ApiOperation(value = "4、对外股权投资-分页列表查询", notes = "4、对外股权投资-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<TbExternalEquity>> queryPageList(TbExternalEquity tbExternalEquity,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {

        List<TbExternalEquity> statistics = tbExternalEquityService.statistics(tbExternalEquity, req.getParameterMap());
        IPage<TbExternalEquity> page = new Page(pageNo, pageSize);
        page.setTotal(statistics.size());
        int startIndex = (pageNo - 1) * pageSize; // 起始索引
        int endIndex = Math.min(startIndex + pageSize, statistics.size());
        if (startIndex >= endIndex) {
            return Result.OK(page);
        }
        page.setRecords(statistics.subList(startIndex, endIndex));
        return Result.OK(page);
    }

    /**
     * 添加
     *
     * @param tbExternalEquity
     * @return
     */
    @AutoLog(value = "4、对外股权投资-添加")
    @ApiOperation(value = "4、对外股权投资-添加", notes = "4、对外股权投资-添加")
    @RequiresPermissions("externalEquity:tb_external_equity:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody TbExternalEquity tbExternalEquity) {
        tbExternalEquityService.save(tbExternalEquity);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param tbExternalEquity
     * @return
     */
    @AutoLog(value = "4、对外股权投资-编辑")
    @ApiOperation(value = "4、对外股权投资-编辑", notes = "4、对外股权投资-编辑")
    @RequiresPermissions("externalEquity:tb_external_equity:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody TbExternalEquity tbExternalEquity) {
        tbExternalEquityService.updateById(tbExternalEquity);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "4、对外股权投资-通过id删除")
    @ApiOperation(value = "4、对外股权投资-通过id删除", notes = "4、对外股权投资-通过id删除")
    @RequiresPermissions("externalEquity:tb_external_equity:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        tbExternalEquityService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "4、对外股权投资-批量删除")
    @ApiOperation(value = "4、对外股权投资-批量删除", notes = "4、对外股权投资-批量删除")
    @RequiresPermissions("externalEquity:tb_external_equity:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.tbExternalEquityService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "4、对外股权投资-通过id查询")
    @ApiOperation(value = "4、对外股权投资-通过id查询", notes = "4、对外股权投资-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<TbExternalEquity> queryById(@RequestParam(name = "id", required = true) String id) {
        TbExternalEquity tbExternalEquity = tbExternalEquityService.getById(id);
        if (tbExternalEquity == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(tbExternalEquity);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param tbExternalEquity
     */
    // @RequiresPermissions("externalEquity:tb_external_equity:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TbExternalEquity tbExternalEquity) {
        return this.exportXls(request, tbExternalEquity, TbExternalEquity.class, "对外股权投资");
    }

    @Resource
    private JeecgBaseConfig jeecgBaseConfig;

    protected ModelAndView exportXls(HttpServletRequest request, TbExternalEquity tbExternalEquity, Class clazz, String title) {
        // Step.1 组装查询条件

        // Step.2 获取导出数据
        List<TbExternalEquity> exportList = tbExternalEquityService.statistics(tbExternalEquity, request.getParameterMap());

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, title);
        mv.addObject(NormalExcelConstants.CLASS, clazz);
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams = new ExportParams("（2024）年度市国资委出资企业无形资产-对外股权投资明细表（汇总）", title);
        exportParams.setImageBasePath(jeecgBaseConfig.getPath().getUpload());
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }
    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("externalEquity:tb_external_equity:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TbExternalEquity.class);
    }

}
